package control;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.BookingDTO;
import model.UserDTO;
import business.BookingManager;
import business.UserManager;

/**
 * Servlet implementation class AddBooking
 */
@WebServlet("/AddBooking")
public class AddBookingServelet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddBookingServelet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		doProcess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		doProcess(request, response);
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		UserSession us = (UserSession) request.getSession(false).getAttribute(
				"USERSESSION");

		String startdate = request.getParameter("timestamp");
		// String Type = request.getParameter("facilityType");
		String period = request.getParameter("period");
		String priority = request.getParameter("priority");
		String reason = request.getParameter("reason");
		String failityID1 = request.getParameter("facilityID");
		int id = us.getUser().getUserID();
		UserManager um= new UserManager();  
		UserDTO user= um.findUser(id);
		System.out.println(priority);

		String address  = "Successful.jsp?actionName=ADD_BOOKING";;

		Integer facilityID = Integer.valueOf(failityID1);
		System.out.println(facilityID);

		BookingDTO book = new BookingDTO();
		book.setFacilityID(facilityID);
		book.setPeriod(Integer.valueOf(period));
		book.setPriority(priority);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		
		Date dStart = null;
		try {
			dStart = df.parse(startdate);
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
		book.setStartDate(dStart);
		book.setUserID(id);
		book.setReason(reason);
		book.setStatus("booked");
		
		BookingManager bm = new BookingManager();
		bm.addBooking(book);
//		ArrayList<BookingDTO> bookings = bm.findBookingbyFacilityID(book.getFacilityID());
//		boolean c = false;
//		if (!bookings.isEmpty()) {
//			Iterator<BookingDTO> i = bookings.iterator();
//			while (i.hasNext()) {
//				BookingDTO b = i.next();
//				if (book.getStartDate().before(b.getStartDate())
//						&& book.getEndDate().before(b.getStartDate())
//						|| book.getStartDate().after(b.getEndDate())) {
//					c = true;
//					break;
//				}}
//				if (c) {
//					bm.addBooking(book);
//				} else {
//				    address = "/NotAvailable.jsp";
//				}
//		}else{
//			bm.addBooking(book);
//		}

		response.sendRedirect(address);
		
		simpleEmail mail = new simpleEmail();  
		
		mail.setFrom(us.getUser().getEmail());
		UserDTO mgr = um.findUser(user.getManagerID());
		  mail.setM_to(mgr.getEmail());
		  mail.setM_text("One booking request from "+user.getUserName()+".Action is required from you. Please login to http://localhost:8080/FacilityBookingSystem/login.jsp");
		  mail.sendMail();
	}
}
